const _ = require('lodash');
/* 当前充电桩总体状态应答/上报 */
exports.run = (info, core, socket, logger) => {
  if (!socket.charging) { return; }
  const Charging = core.getModel('Charging');
  const reader = new core.BufferReader(info.data),
    userIdBuf = reader.read(2),
    cmdIndexBuf = reader.read(2),
    power = reader.read(1),
    temperature = reader.read(1),
    outTemperature = reader.read(1),
    workStatus = reader.read(1),
    chargeableModulesCount = reader.read(1),
    outputActivePower =  reader.read(2),
    outputReactivePower =  reader.read(2),
    systemTotalStatus = reader.read(1),
    systemErrorStatus = reader.read(4),
    acdcErrorStatus =  reader.read(4),
    fanStatus =  reader.read(1),
    airConditionerStatus = reader.read(1),
    heaterStatus = reader.read(1),
    smokeAlarmStatus = reader.read(1),
    vibrationSensorStatus = reader.read(1),
    chargingGunCount = reader.read(1);
    let data = {
      power: (() => {
        switch(power.readUInt8()) {
          case 1: return '正常';
          case 2: return '异常';
          default: return '';
        }
      })(),
      temperature: temperature.readUInt8() - 50,
      outTemperature: outTemperature.readUInt8() - 50,
      workStatus: (() => {
        switch(workStatus.readUInt8()) {
          case 1: return '管理状态';
          case 2: return '服务状态';
          case 3: return '故障状态';
          case 9: return '初始化状态';
          default: return '';
        }
      })(),
      chargeableModulesCount: chargeableModulesCount.readUInt8(),
      chargingGunCount: chargingGunCount.readUInt8(),
      //
      systemTotalStatus: ((byte) => {
        let status = {};
        status['total'] = (byte >> 0) % 2 ? '故障': '正常';
        status['door'] = (byte >> 1) % 2 ? '门开': '关闭';
        status['pilePosition'] = (byte >> 2) % 2 ? '占用': '空闲';
        const work = `${((byte >> 7) % 2)}${((byte >> 6) % 2)}${((byte >> 5) % 2)}${((byte >> 4) % 2)}`;
        if (work == '0000') {
          status['work'] = '独立';
        } else if (work == '0001') {
          status['work'] = '并机';
        } else {
          status['work'] = work;
        }
        return status;
      })(systemTotalStatus[0]),
      systemErrorStatus: ((code) => {
        let codes = [];
        _.times(12, (i) => {
          if ((code & 1 << i) == 1 << i) {
            codes.push(i);
          }
        });
        return codes;
      })(systemErrorStatus.readUInt32LE()),
      acdcErrorStatus: acdcErrorStatus.toString('hex'),
      fanStatus: (() => {
        switch(fanStatus.readUInt8()) {
          case 1: return '停止';
          case 2: return '运行';
          case 3: return '故障';
          default: return '';
        }
      })(),
      airConditionerStatus: (() => {
        switch(airConditionerStatus.readUInt8()) {
          case 1: return '停止';
          case 2: return '运行';
          case 3: return '故障';
          default: return '';
        }
      })(),
      heaterStatus: (() => {
        switch(heaterStatus.readUInt8()) {
          case 1: return '停止';
          case 2: return '运行';
          case 3: return '故障';
          default: return '';
        }
      })(),
      smokeAlarmStatus: (() => {
        switch(smokeAlarmStatus.readUInt8()) {
          case 1: return '停止';
          case 2: return '运行';
          case 3: return '故障';
          default: return '';
        }
      })(),
      vibrationSensorStatus: (() => {
        switch(vibrationSensorStatus.readUInt8()) {
          case 1: return '停止';
          case 2: return '运行';
          case 3: return '故障';
          default: return '';
        }
      })(),
      outputReactivePower: outputActivePower.readUInt16LE() * 0.1,
      outputActivePower: outputReactivePower.readUInt16LE() * 0.1,
      accessed_at: _.now(),
    };
    Charging.savePacket(socket.charging, '0x31', data);
    core.getService('sdk')
        .report(socket.charging, data.systemErrorStatus)
        .catch((err) => {
          logger.error(err);
        });
}

